package com.example.chatroom.controller;

import com.example.chatroom.common.errorcode.ControllerErrorCodeConstants;
import com.example.chatroom.common.exception.ControllerException;
import com.example.chatroom.common.pojo.CommonResult;
import com.example.chatroom.controller.param.UserParam;
import com.example.chatroom.controller.result.FriendListResult;
import com.example.chatroom.controller.result.UserResult;
import com.example.chatroom.service.IUserService;
import com.example.chatroom.service.dto.FriendListDTO;
import com.example.chatroom.service.dto.UserDTO;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    @PostMapping("/login")
    public CommonResult<UserResult>login (@Validated UserParam userParam, HttpSession httpSession){
        log.info("login UserParam:{}", userParam);
        UserDTO userDTO = userService.login(userParam);
        //设置session
        log.info("session userId:{}",userDTO.getId());
        httpSession.setAttribute("userId",(Long)userDTO.getId());
        return CommonResult.success(convertToUserResultLOGIN(userDTO));
    }


    private UserResult convertToUserResultLOGIN(UserDTO userDTO) {
        if(userDTO ==null){
            log.error(ControllerErrorCodeConstants.USER_LOGIN_ERROR.getMsg());
            throw new ControllerException(ControllerErrorCodeConstants.USER_LOGIN_ERROR);
        }
        UserResult userResult =new UserResult();
        userResult.setUserId(userDTO.getId());
        userResult.setUserName(userDTO.getUserName());
        if(StringUtils.hasLength(userDTO.getUserToken())){
            userResult.setUserToken(userDTO.getUserToken());
        }
        return userResult;
    }

    @PostMapping("/register")
    public CommonResult<UserResult>register (@Validated UserParam userParam){
        log.info("register UserParam:{}", userParam);
        UserDTO userDTO = userService.register(userParam);
        return CommonResult.success(convertToUserResultREGISTER(userDTO));
    }

    private UserResult convertToUserResultREGISTER(UserDTO userDTO) {
        if(userDTO ==null){
            log.error(ControllerErrorCodeConstants.USER_REGISTER_ERROR.getMsg());
            throw new ControllerException(ControllerErrorCodeConstants.USER_REGISTER_ERROR);
        }
        UserResult userResult =new UserResult();
        userResult.setUserId(userDTO.getId());
        userResult.setUserName(userDTO.getUserName());
        if(StringUtils.hasLength(userDTO.getUserToken())){
            userResult.setUserToken(userDTO.getUserToken());
        }
        return userResult;
    }

    /**
     * 注销用户
     * @return
     */
    @RequestMapping("/exit")
    public CommonResult<Boolean> exit(HttpServletRequest request){
        log.info("注销用户");
        userService.exit(request);
        return CommonResult.success(true);
    }

    @GetMapping("/userInfo")
    public CommonResult<UserResult> getUserInfo(HttpServletRequest request){
        log.info("getUserInfo : 获取用户信息");
        UserDTO userDTO=userService.getUserInfo(request);
        return CommonResult.success(convertToUserResultUserInfo(userDTO));
    }

    private UserResult convertToUserResultUserInfo(UserDTO userDTO) {
        if(userDTO ==null){
            log.error(ControllerErrorCodeConstants.USER_GET_USER_INFO_ERROR.getMsg());
            throw new ControllerException(ControllerErrorCodeConstants.USER_GET_USER_INFO_ERROR);
        }
        UserResult userResult =new UserResult();
        userResult.setUserId(userDTO.getId());
        userResult.setUserName(userDTO.getUserName());
        if(StringUtils.hasLength(userDTO.getUserToken())){
            userResult.setUserToken(userDTO.getUserToken());
        }
        return userResult;
    }
}
